<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:45
 */

namespace App\Services;


use App\Models\Apart;
use App\Models\Contract;
use App\Models\ProgramApart;
use App\Models\ProgramUser;
use App\Models\Region;
use App\Models\User;
use App\Models\UserApart;

class ApartService extends Service
{
    public static function getApartList($limit, $offset, $order = 'desc', $option = [])
    {
        $query = Apart::where('status', '>', 0);
        if (count($option) && count($option['query'])) {
            foreach ($option['query'] as $search) {
                $query = $query->where($search['key'], $search['operator'], $search['value']);
            }
        }
        $total = $query->count();
        $aparts = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($aparts as $apart) {
            $region = Region::where('id', $apart->region)->first();
            $data[] = [
                'id'            => $apart->id,
                "name"          => $apart->name,
                "region"        => $apart->region,
                "region_name"   => $region->name,
                "type"          => $apart->type,
                "orientations"  => $apart->orientations,
                "area"          => $apart->area,
                "is_program"    => $apart->is_program,
                "status"        => $apart->status
            ];
        }

        return [
            'total'      => $total,
            'rows'       => $data,
            'limit'      => $limit,
            'offset'     => $offset,
            'page'       => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getProgramApartList($programId, $limit, $offset, $order)
    {
        $query = ProgramApart::where('program_id', $programId)->where('status', 1);
        $total = $query->count();
        $programAparts = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($programAparts as $programApart) {
            $apart = Apart::where('id', $programApart->apart_id)->first();
            $region = Region::where('id', $apart->region)->first();
            $data[] = [
                'id'            => $programApart->id,
                'apart_id'            => $programApart->apart_id,
                "name"          => $programApart->apart_name,
                'type' => $apart->type,
                'region' => $region->name,
                'area' => $apart->area,
                'rent' => $apart->rent,
                'other' => $apart->other,
                "created_at"          => local_date($programApart->created_at, 'Y-m-d H:i')
            ];
        }

        return [
            'total'      => $total,
            'rows'       => $data,
            'limit'      => $limit,
            'offset'     => $offset,
            'page'       => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getUnselectAparts()
    {
        $aparts = Apart::where('status', 1)->where('is_program', 0)->get();
        $data = [];
        foreach ($aparts as $apart) {
            $data[] = [
                'id' => $apart->id,
                'name' => $apart->name
            ];
        }

        return ['rows' => $data];
    }

    public static function deleteProgramApart($id)
    {
        $programApart = ProgramApart::where('id', $id)->first();
        $programApart->status = 0;
        $programApart->updated_at = date('Y-m-d H:i:s', time());
        $programApart->save();
    }

    public static function getTeacherApartList($limit, $offset, $order)
    {
        $query = Apart::where('status', 2)->where('is_program', 0);
        $total = $query->count();
        $aparts = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($aparts as $apart) {
            $region = Region::where('id', $apart->region)->first();
            $data[] = [
                'id' => $apart->id,
                'name' => $apart->name,
                'region' => $apart->region,
                'region_name' => $region->name,
                'type' => $apart->type,
                'area' => $apart->area,
                'rent' => $apart->rent
            ];
        }

        return [
            'total'      => $total,
            'rows'       => $data,
            'limit'      => $limit,
            'offset'     => $offset,
            'page'       => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getApartDetail($id)
    {
        $apart = Apart::where('id', $id)->first();

        if (!$apart) {
            return [];
        }

        return [
            'data' => [
                'id'           => $apart->id,
                "name"         => $apart->name,
                "region"       => $apart->region,
                "type"         => $apart->type,
                "address"      => $apart->address,
                "area"         => $apart->area,
                "standard"  => $apart->standard,
                "orientations" => $apart->orientations,
                "rent"         => (float)$apart->rent,
                "other"        => $apart->other,
                "status"       => $apart->status
            ]
        ];
    }

    public static function saveApart($data)
    {
        $apart = new Apart();
        if (array_key_exists('id', $data) && $data['id']) {
            $apart = Apart::where('id', $data['id'])->first();
        } else {
            $apart->created_at = date('Y-m-d H:i:s', time());
        }
        $apart->name = $data['name'];
        $apart->region = $data['region'];
        $apart->type = $data['type'];
        $apart->address = $data['address'];
        $apart->area = $data['area'];
        $apart->standard = $data['standard'];
        $apart->orientations = $data['orientations'];
        $apart->rent = $data['rent'];
        $apart->other = $data['other'];
        $apart->status = $data['status'];
        $apart->updated_at = date('Y-m-d H:i:s', time());
        $apart->save();
    }

    public static function deleteApart($id)
    {
        $apart = Apart::where('id', $id)->first();
        $apart->status = 0;
        $apart->updated_at = date('Y-m-d H:i:s', time());
        $apart->save();
    }

    public static function getAparts()
    {
        $aparts = Apart::where('status', 1)->get();
        $data = [];
        foreach ($aparts as $apart) {
            $data[] = [
                'id' => $apart->id,
                'name' => $apart->name
            ];
        }
        return ['rows' => $data];
    }

    public static function select($programId, $apartId, $userId)
    {
        $apart = Apart::where('id', $apartId)->first();
        $user = User::where('id', $userId)->first();
        $userApart = new UserApart();
        $userApart->program_id = $programId;
        $userApart->apart_id = $apartId;
        $userApart->apart_name = $apart->name;
        $userApart->user_id = $userId;
        $userApart->realname = $user->realname;
        $userApart->status = 0;
        $userApart->created_at = date('Y-m-d H:i:s', time());
        $userApart->updated_at = date('Y-m-d H:i:s', time());
        $userApart->save();
        $programUser = ProgramUser::where('program_id', $userApart->program_id)->where('user_id', $userApart->user_id)->first();
        $programUser->state = 1;
        $programUser->updated_at = date('Y-m-d H:i:s', time());
        $programUser->save();
    }

    public static function getUserApart($userId)
    {
        $userApart = UserApart::where('status', 1)->where('user_id', $userId)->orderBy('id', 'desc')->first();
        if (!$userApart) {
            return [];
        }

        return [
            'data' => [
                'id' => $userApart->id,
                'apart_id' => $userApart->apart_id,
                'apart_name' => $userApart->apart_name,
                'user_id' => $userApart->user_id,
                'realname' => $userApart->realname
            ]
        ];
    }

    public static function getUserApartList($limit, $offset, $order = 'desc', $apartId = null)
    {
        $query = UserApart::where('program_id', 0)->where('status', 1);
        if ($apartId) {
            $query = $query->where('apart_id', $apartId);
        }
        $total = $query->count();
        $userAparts = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($userAparts as $userApart) {
            $data[] = [
                'id' => $userApart->id,
                'user_id' => $userApart->user_id,
                'realname' => $userApart->realname,
                'apart_id' => $userApart->apart_id,
                'apart_name' => $userApart->apart_name,
                'status' => $userApart->status
            ];
        }

        return [
            'total'      => $total,
            'rows'       => $data,
            'limit'      => $limit,
            'offset'     => $offset,
            'page'       => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function saveUserApart($apartId, $userId)
    {
        $userApart = new UserApart();
        $userApart->user_id = $userId;
        $user = User::where('id', $userId)->first();
        $userApart->realname = $user->realname;
        $userApart->apart_id = $apartId;
        $apart = Apart::where('id', $apartId)->first();
        $userApart->apart_name = $apart->name;
        $userApart->status = 1;
        $userApart->created_at = date('Y-m-d H:i:s', time());
        $userApart->updated_at = date('Y-m-d H:i:s', time());
        $userApart->save();
    }

    public static function changeUserApartStatus($data)
    {
        $userApart = UserApart::where('id', $data['id'])->first();
        $userApart->status = $data['status'];
        $userApart->updated_at = date('Y-m-d H:i:s', time());
        $userApart->save();
        if ($userApart->status == 1) {
            $programUser = ProgramUser::where('program_id', $userApart->program_id)->where('user_id', $userApart->user_id)->first();
            $programUser->state = 2;
            $programUser->updated_at = date('Y-m-d H:i:s', time());
            $programUser->save();
            $contract = new Contract();
            $contract->program_id = $userApart->program_id;
            $contract->apart_id = $userApart->apart_id;
            $contract->apart_name = $userApart->apart_name;
            $contract->user_id = $userApart->user_id;
            $contract->realname = $userApart->realname;
            $apart = Apart::where('id', $userApart->apart_id)->first();
            $contract->rent = $apart->rent;
            $contract->created_at = date('Y-m-d H:i:s', time());
            $contract->updated_at = date('Y-m-d H:i:s', time());
            $contract->save();
        }
    }

    public static function releaseUserApart($apartId, $uid)
    {
        $userApart = UserApart::where('apart_id', $apartId)->where('user_id', $uid)->first();
        $programUser = ProgramUser::where('program_id', $userApart->program_id)->where('user_id', $userApart->user_id)->first();
        $programUser->state = 0;
        $programUser->updated_at = date('Y-m-d H:i:s', time());
        $programUser->save();
        UserApart::where('apart_id', $apartId)->where('user_id', $uid)->delete();
    }

    public static function import($data)
    {
        for ($i = 1; $i < count($data); $i ++) {
            $apart = new Apart();
            $apart->name = $data[$i][0] == null ? '' : $data[$i][0];
            $apart->region = $data[$i][1] == null ? 0 : $data[$i][1];
            $apart->type = $data[$i][2] == null ? 0 : $data[$i][2];
            $apart->address = $data[$i][3] == null ? '' : $data[$i][3];
            $apart->area = $data[$i][4] == null ? 0 : $data[$i][4];
            $apart->standard = $data[$i][5] == null ? 0 : $data[$i][5];
            $apart->orientations = $data[$i][6] == null ? 0 : $data[$i][6];
            $apart->other = $data[$i][7] == null ? '' : $data[$i][7];
            $apart->rent = $data[$i][8] == null ? 0 : $data[$i][8];
            $apart->status = 1;
            $apart->is_program = 0;
            $apart->created_at = date('Y-m-d H:i:s', time());
            $apart->updated_at = date('Y-m-d H:i:s', time());
            $apart->save();
        }
    }
}